package com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou.CYInStorageProductionOverViewActivity;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageProductionOverviewBean;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/4/3 15:50
 * @email siyu.wang@inossem.com
 */
public class CYProductionOverViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<CYInStorageProductionOverviewBean.OverViewBean.ItemListBean> list;
    private LayoutInflater inflater;
    private CYInStorageProductionOverViewActivity.InStorageShoppingOverviewListener listener;
    private String mPlanOrderNumber, mFounder, mOrderType, mRemark,mCondition;

    public CYProductionOverViewAdapter(Context context , String condition , List<CYInStorageProductionOverviewBean.OverViewBean.ItemListBean> list , CYInStorageProductionOverviewBean.OverViewBean data , CYInStorageProductionOverViewActivity.InStorageShoppingOverviewListener listener ) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        this.mCondition = condition;
        this.list = list;
        this.listener = listener;
        mPlanOrderNumber = data.getPurchase_order_code();
        mFounder = data.getCreate_user_name();
        mOrderType = data.getPurchase_order_type_name();
        mRemark = data.getRemark();
//        mSupplier = data.getSupplier_name();
//        mCreateDate = data.getCreate_time();
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.cy_view_in_storage_production_item_list_first_item, null);
            TextView planOrderNumber = (TextView) convertView.findViewById(R.id.planOrderNumber);
            TextView founder = (TextView) convertView.findViewById(R.id.founder);
            TextView orderType = (TextView) convertView.findViewById(R.id.orderType);
            TextView remark = (TextView) convertView.findViewById(R.id.remark);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            materielCode.setHint("请输入物料编码");
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            planOrderNumber.setText(mPlanOrderNumber);
            founder.setText(mFounder);
            orderType.setText(mOrderType);
            remark.setText(mRemark);
            materielCode.setText(mCondition);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
//            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, materielCode);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    if (!TextUtils.isEmpty(materielCode.getText().toString())) {
                        listener.onNoDoubleClickListener(materielCode.getText().toString());
                    }
                }
            });
        } else {
            CYInStorageProductionOverviewBean.OverViewBean.ItemListBean item = list.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.cy_view_in_storage_production_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView  , item);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.materialCode = (TextView) view.findViewById(R.id.materialCode);
        viewHolder.materialDescribe = (TextView) view.findViewById(R.id.materialDescribe);
        viewHolder.inStorageNumber = (TextView) view.findViewById(R.id.inStorageNumber);
        viewHolder.materialCodeLayout = (PercentRelativeLayout) view.findViewById(R.id.materialCodeLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view , CYInStorageProductionOverviewBean.OverViewBean.ItemListBean item) {
        viewHolder.materialCode.setText(item.getMat_code());
        viewHolder.materialDescribe.setText(item.getMat_name());
        viewHolder.inStorageNumber.setText(TextUtils.isEmpty(item.getInStorageNumber()) ? "请输入数量" : item.getInStorageNumber() + item.getName_zh());
        if (item.getInStorageNumber() != null) {
            if (Float.parseFloat(item.getInStorageNumber()) != 0) {
                viewHolder.materialCodeLayout.setBackgroundResource(R.color.test_green);
            }
            else {
                viewHolder.materialCodeLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
            }
        } else {
            viewHolder.materialCodeLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
//                if (mInStorageReceiveListener.onCheckData()) {
//                Intent intent = new Intent(context, CYInStorageShoppingDetailActivity.class);
//                intent.putExtra(InStorageExtra.MAT_ID, item.getMat_id());
//                context.startActivity(intent);
                listener.onViewClickListener(item.getMat_id());
            }
//            }
        });
    }

    private class ViewHolder {
        private TextView materialCode, materialDescribe, inStorageNumber;

        private PercentRelativeLayout materialCodeLayout;
    }
}
